package br.ufrn.execicios.jsf.commons;

import java.io.IOException;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

public class FacesUtil {
	
	public static void adicionarMensagem(Severity tipo, String msg) {
		FacesContext.getCurrentInstance().addMessage(null, 
				new FacesMessage(tipo, msg, msg));
	}
	// Getters -----------------------------------------------------------------------------------

    public static Object getApplicationMapValue(String key) {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get(key);
    }

    // Setters -----------------------------------------------------------------------------------

    public static void setApplicationMapValue(String key, Object value) {

        FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(key, value);
    }
    
    public static void clearSessionValues(){
    	FacesContext.getCurrentInstance().getExternalContext().getSessionMap().clear();
    }
    public static void setSessionValue(String Key, Object value){
    	FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Key, value);
    }
    public static Object getSessionValue(String key)
     {
    	return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(key);
    }

	public static void redirectTo(String url) {
		
	    try {
	    	ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
			ec.redirect(ec.getRequestContextPath() + url);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//FacesContext ctx = FacesContext.getCurrentInstance();

		//ctx.getApplication().getNavigationHandler().handleNavigation(ctx, null, url);
		
	}

}
